

//key1 -> ENT1/GPF1  GPFCON[3:2]=0 :input
//key2 -> ENT4/GPF4  GPFCON[9:8]=0 :input
//key3 -> ENT2/GPF2  GPFCON[5:4]=0 :input
//key4 -> ENT0/GPF0  GPFCON[1:0]=0 :input
#define GPFCONF  ((volatile unsigned long*)0x56000050)
#define GPFDATA  ((volatile unsigned long*)0x56000054)

#define KEY1_MASK (0x3 << 2)
#define KEY2_MASK (0x3 << 8)
#define KEY3_MASK (0x3 << 4)
#define KEY4_MASK (0x3 << 0)

extern void init_led();
extern void set_led(int led_no, int is_set_on);

void init_key() {
    *GPFCONF &= ~KEY1_MASK; //set bit[3:2] to 00, config GPF1(key1) as input
    *GPFCONF &= ~KEY2_MASK; //set bit[9:8] to 00, config GPF4(key2) as input
    *GPFCONF &= ~KEY3_MASK; //set bit[5:4] to 00, config GPF2(key3) as input
    *GPFCONF &= ~KEY4_MASK; //set bit[1:0] to 00, config GPF0(key4) as input
}

int main() {
    char i = 0;

    init_led();
    init_key();

    while(1) {
        if (*GPFDATA & (0x1 << 1)) { //read GPFDATA[1], so key state is:up
            set_led(1, 0); //set led1 off
        } else {
            set_led(1, 1); //set led on
        }  

        if (*GPFDATA & (0x1 << 4)) { //read GPFDATA[4], so key state is:up
            set_led(2, 0); //set led2 off
        } else {
            set_led(2, 1); //set led2 on
        }  

        if (*GPFDATA & (0x1 << 2)) { //read GPFDATA[2], so key state is:up
            set_led(3, 0); //set led1 off
        } else {
            set_led(3, 1); //set led on
        }  

        if (*GPFDATA & (0x1 << 0)) { //read GPFDATA[0], so key state is:up
            set_led(4, 0); //set led1 off
        } else {
            set_led(4, 1); //set led on
        }  

    }
    return 0;
}
